// pages/wode/setPic.js
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    storeImage:[], //店铺轮播图
    deskImage:[], //扫码点餐轮播图
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.hideShareMenu()
    this.getImage()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  //上传图片
  uploadImg(e) {
    let key = e.currentTarget.dataset.name
    let that = this
    var images=this.data[key]
    wx.chooseMedia({
      count:1,
      success (res) {
        console.log(res.tempFiles[0].tempFilePath)
        const tempFilePaths = res.tempFiles[0].tempFilePath
        wx.uploadFile({
          url: app.agriknow._baseUrl+'ht/user/uploadImage', //仅为示例，非真实的接口地址
          filePath: tempFilePaths,
          name: 'file',
          formData: {
            'user': 'test'
          },
          success (res){
            that.setData({
              [key]:that.data[key].concat(JSON.parse(res.data).data)
            })
            
          }
        })
      }
    })
  },

  getImage:function(){
    var that=this
    var data={}
    data.id=wx.getStorageSync('userInfo').storeId
    app.agriknow.commRequest('ht/store/imageAdmin',data).then(res=>{
        var datas=res.data
        var storeImage = datas.storeImage
        var deskImage = datas.deskImage
        
        // && !Array.isArray(storeImage)
        if(storeImage){
          storeImage=storeImage.split(',')
        }
        if(deskImage){
          deskImage=deskImage.split(',')
        }
        that.setData({
          storeImage:storeImage || [],
          deskImage:deskImage || []
        })
    })
  },
  updImageAdmin:function(){
    var that=this
    var data={}
    data.id=wx.getStorageSync('userInfo').storeId
    data.storeImage=this.data.storeImage.join(',')
    data.deskImage=this.data.deskImage.join(',')
    app.agriknow.commRequest('ht/store/updImageAdmin',data).then(res=>{
       if(res.code=='000000'){
         wx.showToast({
           title: '保存成功',
           icon:'none',
           duration:1500,
           mask:true
         })
         setTimeout(function(){
           wx.navigateBack()
         },1500)
       }else{
        wx.showToast({
          title: '保存失败',
          icon:'none',
          duration:1500,
          mask:true
        })
       }
    })
  },

  delImg:function(e){
    console.log(e)
    var indexs=e.currentTarget.dataset.index
    var name=e.currentTarget.dataset.name
    var images=this.data[name]
    images.splice(indexs,1)
   
    this.setData({
      [name]: images
    })
  },
  toPreview:function(e){
    var indexs=e.currentTarget.dataset.index
    var name=e.currentTarget.dataset.name
    var images=this.data[name]
    wx.previewImage({
      urls: images,
      current:images[indexs]
    })
  }


})